<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/11/6
 * Time: 14:45
 */

namespace app\admin\controller;


use app\admin\common\Base;
use think\facade\Request;
use think\Facade\Session;

class Login extends Base
{
    public function initialize()
    {
        if ($this->ifLogin()){
            return $this->redirect('admin/user/userlist');
        }
    }

    public function index(){
        $this->assign('title','后台登录');
        return $this->view->fetch();
    }

    public function checkLogin(){
        $email = Request::param('email');
        $password = Request::param('password');
        $user = \app\admin\model\User::get(function ($query) use ($email){
            $query->field('id,is_admin,name,email,password,status')->where('email','=',$email);
        });
        if (empty($user)){
            return $this->error('邮箱或者密码不正确');
        }
        if ($user['password'] != sha1($password)){
            return $this->error('邮箱或者密码不正确');
        }
        if ($user['status'] == 0){
            return $this->error('非管理员或者已经禁用账号');
        }
        Session::set('admin_id', $user['id']);
        Session::set('admin_name', $user['name']);
        Session::set('is_admin', $user['is_admin']);
        return $this->success('登录成功','admin/index/index');
    }
}